﻿
/**
 * @author:Ray
 * get the String before split
 * s:String
 * split ","
 */
function getFrontString(s,split){
       var sub="";
       var strIndex = s.indexOf(split);
       if(strIndex!=-1){
         sub = s.substring(0,strIndex);
       } 
      return sub;
}

/**
 * @author:Ray
 * get the String after split
 * s:String
 * split ","
 */
function getSubString(s,listSeparator) {
	  var sub="";
      var strIndex = s.indexOf(listSeparator);
      if(strIndex!=-1){
        sub = s.substring(strIndex+1);
      } 
      return sub;
}
    


/**
 * @author:Ray
 * fill the selection 
 * selectionName: selection id
 * data:data source,data:Array();
 */   
function fillSelection(selectionName,data){
      clearSelection(selectionName);
      var models = document.getElementById(selectionName);
      var len = data.length;
      for(var i=0;i<len;i++){
         var value = data[i];
         var optionValue = getFrontString(value,",");
         var optionDisplayName = getSubString(value,",");
         option = document.createElement("option");
         option.text = optionDisplayName;
		 option.value = optionValue;
         models.add(option);
      }
}


function ttt(){
alert("---------------1")
}


function clearSelection(selectionName){
	var models = document.getElementById(selectionName);
      var option = null;
      option = document.createElement("option");
      for(var i=0;i<models.options.length;i++){
      	if(models.options[i].value == ""){
      	    option.text = models.options[i].text;
            option.value = models.options[i].value;
            break;
      	}
      }
	  while(length!=0){  
         var length = models.options.length;
         for(var i=0;i<length;i++){
         		models.options.remove(i);
         }
         length=length/2;
     }
     models.add(option);
}


/**
 *select current all checkbox
 @author:Ray 
*/
function select_all_none(tablename){
    var temptable;  
    temptable = document.getElementById(tablename);
    if (temptable.rows.length == "1"){
      return;
    }
    var i;
    for(i=temptable.rows.length-1;i>=1;i--){
	temptable.rows(i).cells(0).firstChild.checked = temptable.rows(0).cells(0).firstChild.checked;
    }	
}

/**
 *submit form
 *@author:Ray
 */
function submitForm(formId,hiddenFunctionId,hiddenValue){
   document.getElementById(hiddenFunctionId).value="S"+hiddenValue;
   document.getElementById(formId).submit();
}


function emptyValidate(inputComponent,errorMessage) {
	  var t = document.getElementById(inputComponent).value;
      t = t.replace(/(^\s*)|(\s*$)/g, "");
	  if(t==""){
	    window.alert(errorMessage);
    	document.getElementById(inputComponent).focus();
	    return false;
	  }
	  document.getElementById(inputComponent).value = t;
	  return true;
}

function emptyValidate2(inputComponent,errorMessage) {
	  var t = document.getElementById(inputComponent).value;
      t = t.replace(/(^\s*)|(\s*$)/g, "");
	  if(t==""){
	    window.alert(errorMessage);
    	
	    return false;
	  }
	  document.getElementById(inputComponent).value = t;
	  return true;
}
/**
 *?????
 @author:he.shen       modify by zheng.liu
*/
function selectAll(identify){
   var i;
   if (document.getElementById(identify+'Checkbox')!=null)
       document.getElementById(identify+'Checkbox').checked=document.getElementById('checkAll').checked;
   for(i=0;i<=50;i++){
       if (document.getElementById(identify+'Checkbox$'+i)!=null)
          document.getElementById(identify+'Checkbox$'+i).checked=document.getElementById('checkAll').checked;
   }
}

/**
 *  ?????
 @author:he.shen      modify by zheng.liu
*/
function selectAllPage(identify){
   var i;
   document.getElementById('checkAll').checked=document.getElementById('checkTotalPage').checked;
   if (document.getElementById(identify+'Checkbox')!=null)
       document.getElementById(identify+'Checkbox').checked=document.getElementById('checkTotalPage').checked;
   for(i=0;i<=50;i++){
       if (document.getElementById(identify+'Checkbox$'+i)!=null)
          document.getElementById(identify+'Checkbox$'+i).checked=document.getElementById('checkTotalPage').checked;
   }
}



//validate selection
function selectionValidate(inputComponent,errorMessage,compareValue){
	  if(document.getElementById(inputComponent).value==compareValue){
	    alert(errorMessage);
    	document.getElementById(inputComponent).focus();
	    return false;
	  }
} 

/**
 *table list should at least checked one item
 *@author:Ray
 *2006-04-19
 */
function tCheckboxAtLeastCheckedOne(inputComponent,errorMessage){
     var tNode=document.getElementById(inputComponent);
     var trNode=tNode.getElementsByTagName("tr");
     var checkedNum=0;
     for(i=1 ;i<trNode.length;i++){
	     var iCheckbox=trNode[i].getElementsByTagName("input"); 
	     var len=iCheckbox.length;
	     for(j=0;j<len;j++){
	        if(iCheckbox[j].checked){
	          checkedNum++;
	        }
		 }
	  }
    if(checkedNum==0){
       alert(errorMessage);
       return false;    
    }else {
    	return true;
    }
}

/**
 *only allow selecd one
 *@author:Ray
 *2006-04-19
 */
function tCheckboxCheckedOnlyOne(inputComponent,errorMessage){
     var tNode=document.getElementById(inputComponent);
     var trNode=tNode.getElementsByTagName("tr");
     var checkedNum=0;
     for(i=1 ;i<trNode.length;i++){
	     var iCheckbox=trNode[i].getElementsByTagName("input"); 
	     var len=iCheckbox.length;
	     for(j=0;j<len;j++){
	        if(iCheckbox[j].checked){
	          checkedNum++;
	        }
		 }
	  }
    if(checkedNum != 1){
       alert(errorMessage);  
       return false;
    }
    else{
    	return true;
    }
}


/**
row line color
*/
/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;
function setPointer(theRow, theAction, thePointerColor, theMarkColor){
    var theRowNum = "";
	var theDefaultColor = "";
    if (theRow.rowIndex % 2 == 0){
      theRowNum = "4505447c-c6c6-21aa-6be0-42af184c1b16";
      theDefaultColor = "#fdfdfd";
    } else {   
      theRowNum = "e16c15c2-7113-80c4-b249-42af18ff28ce";
      theDefaultColor = "#f1f1f1";
    }
    var theCells = null;
    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '') || typeof(theRow.style) == 'undefined') {
        return false;
    }
    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined' && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3
    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == '' || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end

/*
 * mobile validator
 */         
function ismobileValidateNumber(inputComponent,errorMessage){
  var s = document.getElementById(inputComponent).value;
  var len = s.length;
  if(len==0) {
    return true;
  }
  if(letterNumberValidate(inputComponent,errorMessage) == false){
  	return false;
  }
  var regv = /^(01|91|1)(\d{10})$/;
  if(!regv.test(s)){
    alert(errorMessage);
    document.getElementById(inputComponent).focus();
    document.getElementById(inputComponent).select();
   	return false;
  }    
  return true;
}
/*
 * tel validator
 */ 
function PhoneValidate(inputComponent,errorMessage) {
  var str = document.getElementById(inputComponent).value;
  var reg=/(^[0-9]{3,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/
  if(!reg.test(str)){
    alert(errorMessage);
    document.getElementById(inputComponent).focus();
    document.getElementById(inputComponent).select();
   	return false;
  }
  return true;
}
	
	

/**
 * 
 */ 
function txtBlur(object,length,message1){
  var tempStr = "";
  if(object.value.len() > length){
    alert(message1+length);
    object.focus();
  } 
}

/*
 * email Validator
 */
function emailFormatValidate(inputComponent,errorMessage) {
  var s = document.getElementById(inputComponent).value;
  s=trim(s);
  var len = s.length;
  if(len==0) {
    return true;
  }
  var regv = /^[^@](\S)*@((\w+\.)|(\w+\-+\w+\.))+([a-z]|[A-Z])+$/;
  if(!regv.test(s)){
    alert(errorMessage);
    document.getElementById(inputComponent).focus();
    document.getElementById(inputComponent).select();
	return false;
  }
  return true;
} 

/**
 * postCode validat
 */
function isPostCode(inputComponent,errorMessage){
	var s = document.getElementById(inputComponent).value;
	s=trim(s);
	var len = s.length;
	if(len == 0){
		return true;
	}
	var reg = /^\d{6}$/;
	if(!reg.test(s)){
        alert(errorMessage);
        document.getElementById(inputComponent).focus();
        document.getElementById(inputComponent).select();
   	    return false;
  }
	return true;
}

/*
 * 
 */
function letterNumberValidate(inputComponent,errorMessage){
    var s = document.getElementById(inputComponent).value;
    s=trim(s);
    var len = s.length;
    if(len == 0) return true;
	else {
           for(i=0;i<len;i++){
           	 var d = s.substring(i,i+1).charCodeAt(0);
           	 if (!((d >= 48) && (d <= 57))){
        		alert(errorMessage);
        		return false;
     		}  
           }
	}
    return true;
}


 
/**
 * 
 */
function trim(s) {
  //var s = document.getElementById(inputComponent).value;
  s = s.replace(/(^\s*)|(\s*$)/g, "");
  //document.getElementById(inputComponent).value = s; 
  return s;
}

/**
 * 
 */
function isNull(inputComponent){
	var s = document.getElementById(inputComponent).value;
	s = s.replace(/(^\s*)|(\s*$)/g, "");
	document.getElementById(inputComponent).value = s;
	if(s==null || s==""){
		return true;
	}
	return false;
}



/**
 * replace backspace with delim
 */
function replaceBackWithDelim(inputComponent,delim){
	if(isNull(inputComponent)) {
		return;
	}
	var value = document.getElementById(inputComponent).value;
	value = value.replace(/(^\s*)|(\s+)|(\s*$)/g, delim);
	document.getElementById(inputComponent).value=value;
}

/**
 * RadioGroup
 */
function getRadioValue(inputComponent,radioNumber){
	 s = document.getElementsByName(inputComponent);
	 a = radioNumber;
	 for(i=0;i<a;i++){
	 	if(s[i].checked) return s[i].value;
	 }
	 return "";
}
/**
 * number validator
 */
 function isNumeric(inputComponent,errorMessage){ 
      var s = document.getElementById(inputComponent).value;
      var newPar=/^(-|\+)?\d+(\.\d+)?$/
      var len = s.length;
      if(len ==0){
        return true;
      }else if(!newPar.test(s)){
          alert(errorMessage); 
          return false;
      }else if(s.toString().indexOf(".") != -1){
      
               if(s.toString().split(".")[1].length>4){
                    alert(errorMessage);
                    return false
               }
      }else{
      	  return true;
      }   
 } 
 /**
*@ author he.shen
* ??????????"."????
*/
function isFloat(inputComponent,errorMessage){ 
     var s = document.getElementById(inputComponent).value;
      var newPar=/^\d+(?=\.\d+$|$)/
      var len = s.length;
      if(!newPar.test(s)){
          alert(errorMessage); 
          return false;
      }else{
      	  return true;
      }   
      
}

 /**
 * @author:lk
 * number and letter validator
 */
 function numberOrLetterValidate(inputComponent,errorMessage){
     var s = document.getElementById(inputComponent).value;
     s = trim(s);
     var reg = /^[0-9a-zA-Z]+$/;
     if(!reg.test(s)){
        alert(errorMessage);
        document.getElementById(inputComponent).focus();
        document.getElementById(inputComponent).select();
   	    return false;
     }
     return true;
 }

/*????????????????*/
function stringLengthValidate(inputComponent,min,minErrorMessage,max,maxErrorMessage) {
  var s = document.getElementById(inputComponent).value;
  var len = s.length;
  if(len==0) {
      return true;
  }
  var maxLength = parseInt(max);
  var minLength = parseInt(min);
  if(minLength>maxLength||minLength<0||maxLength<0) {
        return false;
  }
  if(len<minLength) {
      alert(minErrorMessage);           
      document.getElementById(inputComponent).focus();
      document.getElementById(inputComponent).select();
      return false;       
  } else if(len>maxLength) {
      alert(maxErrorMessage);
      document.getElementById(inputComponent).focus();
      document.getElementById(inputComponent).select();
      return false;       
  }
}
/**
* get the real length of string
*/
function strlen(str)
{var i;
var len;
len = 0;
for (i=0;i<str.length;i++)
{
if (str.charCodeAt(i)>255) len+=2; else len++;
}
return len;
}

/**
 *length <len
 *lilei.ma 2006-09-07
 */
function stringLength(inputComponent,len,errorMessage){
   var s = document.getElementById(inputComponent).value;
   s = s.replace(/(^\s*)|(\s*$)/g, "");
   document.getElementById(inputComponent).value=s;
   var sLen = strlen(s);
   var maxLength = parseInt(len);
   if(sLen>maxLength){
    alert(errorMessage);   
    document.getElementById(inputComponent).focus();
    document.getElementById(inputComponent).select();  
    return false;      
   }else{
    return true;
   }
}
/**
 *??????????hh:ss:mm
 *lilei.ma
 *2006-09-07
 */
function isTime(inputComponent,errorMessage) 
      { 
        var a= document.getElementById(inputComponent).value;
        var a = a.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/); 
        if (a == null) {window.alert(errorMessage); return false;} 
        if (a[1]>24 || a[3]>60 || a[4]>60) 
        { 
          window.alert(errorMessage); 
          document.getElementById(inputComponent).focus();
          document.getElementById(inputComponent).select();  
          return false 
        } 
        return true; 
      } 
      

/**
 * ????????????div??
 */
function expand(img,div) {
    if(document.getElementById(div) != null) {
  			if(document.getElementById(div).style.display == "none") {		    
		    		document.getElementById(div).style.display = "";
					img.src = "images/common/basic_search.gif";
					img.alt = "close";
				} else {
    			document.getElementById(div).style.display = "none";
					img.src = "images/common/advanced_search.gif";
					img.alt = "open";
				}
		}
}

    /*??????????????,??form??????????????????*/ 
    function moneyFormatValidate(inputComponent,errorMessage){
      var s = document.all[inputComponent].value;
      var len = s.length;
      var i,j=0;
      if(len==0) {
        document.all[inputComponent].value = "";
        return true;
      }
       var regv = /^[^,\u4E00-\u9FA5](\d)*(,\d{3})*(\.\d{1,4})?$/;
       if(!regv.test(s)) {
         alert(errorMessage);           
         document.all[inputComponent].focus();
         document.all[inputComponent].select();
         return false;
	   } else {
            return true;        
       }
    } 
    
    function moneyCheck(inputComponent,errorMessage){
     var s = document.all[inputComponent].value;
      var len = s.length;
      var i,j=0;
      if(len==0) {
        document.all[inputComponent].value = "";
        return true;
      }
       var regv = /^(\d)*(\.\d{1,2})?$/;
       if(!regv.test(s)) {
         alert(errorMessage);           
         document.all[inputComponent].focus();
         document.all[inputComponent].select();
         return false;
	   } else {
            return true;        
       }
    }
    
    
  function isSetNull(inputComponent){
    var s = document.getElementById(inputComponent).value;
     s = s.replace(/(^\s*)|(\s*$)/g, "");
     if(s==null || s==""){
       document.getElementById(inputComponent).focus();
	
	}else{
	    document.getElementById(inputComponent).value =" ";
	    
	
	}
 
}
    
   /**
    * ??????????????
    */
   function checkDate(startDate,endDate,errorMessage){
     date1 = document.getElementById(startDate).value;
     date2 = document.getElementById(endDate).value;
     
     date1 = date1.split(/-|\/|\s/)
     date2 = date2.split(/-|\/|\s/)
    
     var myDate1 = new Date(date1[0],date1[1],date1[2]);
     var myDate2 = new Date(date2[0],date2[1],date2[2]);
     if (myDate1 > myDate2){
          alert(errorMessage)
          return false;
     }
     return true;
    } 
    
    /**
    * @author he.shen
    *???????
    */
	function limitTextAreaMaxLen(o){
	   var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";
	   if(o.getAttribute && o.value.length>nMaxLen){
	   o.value=o.value.substring(0,nMaxLen)
	   }
	}
	
	/**
	* @author lk
	* ip valid
	*/
	function IPValid(inp){
		var inpValue = document.getElementById(inp).value.replace(/^(\s|;)+|(\s|;)+$/g,"");
		if(inpValue!=null && inpValue!=""){
			var temp = document.getElementById(inp).value.replace(/^(\s|;)*|(\s|;)*$/g,"").split(";");
      var result = false;
      var sect;
      for (sect in temp){
      	temp[sect] = temp[sect].replace(/^\s+|\s+$/g,"");  
        var reg = /^\d{1,3}(\.\d{1,3}){3}$/;
      	if(!reg.test(temp[sect])){
      		alert(temp[sect]+" 是错误的ip地址!");
      		result = true;
      	}
      }
      if(result) {
          document.getElementById(inp).focus();
      }else{
        document.getElementById(inp).value = temp.join(";");
      }
		}
  }


